const { name } = require("./package")
const webpack = require("webpack")
const path = require("path")
const MonacoWebpackPlugin = require("monaco-editor-webpack-plugin")
const TerserPlugin = require("terser-webpack-plugin")
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  // publicPath: './',
  publicPath: "/icdfront",
  lintOnSave: true,
  devServer: {
    publicPath: "/icdfront",
    hot: true,
    disableHostCheck: true,
    port: 3050,
    overlay: {
      warnings: false,
      errors: true
    },
    headers: {
      "Access-Control-Allow-Origin": "*"
    },
    // proxy: process.env.VUE_APP_CLOUD_IDE
    proxy: {
      "/icdbackend": {
        target: "http://10.206.32.28:8080/", // 开发
        changeOrigin: true
      },
      "/system": {
        target: "http://100.70.3.152:32762/user-manager/prod-api/", // 开发环境
        changeOrigin: true
      },
      "/auth": {
        target: "http://100.70.3.152:32762/user-manager/prod-api/", // 开发环境
        changeOrigin: true
      }
    }
  },
  css: {
    loaderOptions: {
      sass: {
        sassOptions: {
          outputStyle: "expanded"
        }
      }
    }
  },
  productionSourceMap: false,
  configureWebpack: {
    plugins: [
      // new BundleAnalyzerPlugin(),
      new webpack.ContextReplacementPlugin(
        /moment[\\/]locale$/,
        /^\.\/(zh-cn)$/
      ),
      new MonacoWebpackPlugin({
        languages: ["javascript", "typescript", "html", "css", "json", "sql"]
      })
    ],
    resolve: {
      alias: {
        "bn.js": path.resolve(process.cwd(), "node_modules", "bn.js"),
        "@": resolve("src"),
        "@img": resolve("src/assets/images"),
        "@icon": resolve("src/assets/icons"),
        "@style": resolve("src/assets/styles"),
        "@comp": resolve("src/components")
      }
    },
    output: {
      library: `${name}-[name]`,
      libraryTarget: "umd", // 把微应用打包成 umd 库格式
      jsonpFunction: `webpackJsonp_${name}`
    },
    optimization: {
      minimizer: [
        new TerserPlugin({
          cache: true, // 启用缓存（Webpack 4.x 必须）
          parallel: true, // 多线程加速
          sourceMap: false, // 关闭 SourceMap
          terserOptions: {
            compress: {
              drop_console: true, // 移除所有 console
              drop_debugger: true, // 移除 debugger
              pure_funcs: [
                // 强制移除间接调用
                "console.log",
                "console.info",
                "console.dir",
                "console.time"
              ]
            },
            output: {
              comments: false // 移除注释
            }
          }
        })
      ]
    }
  },
  chainWebpack: (config) => {
    // config.plugin('monaco').use(new MonacoWebpackPlugin());
    config.optimization.splitChunks({
      maxInitialRequests: Infinity,
      minSize: 300000, // 依赖包超过300000bit将被单独打包
      automaticNameDelimiter: "-",
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          name(module) {
            const packageName = module.context.match(
              /[\\/]node_modules[\\/](.*?)([\\/]|$)/
            )[1]
            return `chunk.${packageName.replace("@", "")}`
          },
          priority: 10
        }
      }
    })
    // 确保所有 chunk 应用 Terser 配置
    config.optimization.minimizer("terser").tap((args) => {
      args[0].terserOptions.compress.drop_console = true
      return args
    })
  }
}
